﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemDAO.DAO;

namespace NangLuongTieuDiemBUS.BUS
{
    public class HoaDonBUS
    {
        #region "[Insert]"
        public int Insert(HoaDonDTO hdDto)
        {
            // Kiem tra Business Rule



            // Them
            int IDResult = -1;
            try
            {
                IDResult = HoaDonDAO.Insert(hdDto);
            }
            catch(Exception ex)
            {
                InSert_LoiHeThong("Insert", ex);
            }
            return IDResult;

        }
        #endregion

        #region "[Delete]"
        public bool Delete(int ID)
        {
            // Kiem tra Business Rule



            // Xoa
            bool IDResult = false;
            try
            {
                IDResult = HoaDonDAO.Delete(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Delete", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Update]"
        public bool Update(HoaDonDTO hdDto)
        {
            // Kiem tra Business Rule



            // Sua
            bool IDResult = false;
            try
            {
                IDResult = HoaDonDAO.Update(hdDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Update", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Get all]"
        public List<HoaDonDTO> GetAll()
        {
            List<HoaDonDTO> dshdDto = new List<HoaDonDTO>();
            try
            {
                dshdDto = HoaDonDAO.GetAll();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAll", ex);
            }
            return dshdDto;
        }
        #endregion

        #region "[SelectByPrimaryKey]"
        public HoaDonDTO SelectByPrimaryKey(int ID)
        {
            HoaDonDTO cthdDto = new HoaDonDTO();
            try
            {
                cthdDto = HoaDonDAO.SelectByPrimaryKey(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByPrimaryKey", ex);
            }
            return cthdDto;
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "HoaDonBUS_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            LoiHeThongDAO.Insert(lhtDto);
        }
        #endregion
    }
}
